#include <iostream>
#include <cstring>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::istream;
using std::ostream;


template <class T>
class vector
{
public:
	//迭代器
	typedef T *iterator;
	typedef const T *const_iterator;

	vector()
		: _start(nullptr), _finish(nullptr), _end_of_storage(nullptr)
	{
	}

	//拷贝构造
	vector(const vector<T>& v)
		:_start(nullptr)
		,_finish(nullptr)
		,_end_of_storage(nullptr)
	{
		// _start = new T[v.capacity()];
		// _finish = _start;
		// _end_of_storage = _start + v.capacity();

		// for (size_t i = 0; i < v.size; i++)
		// {
		// 	*_finish = v[i];
		// 	++_finish;
		// }

		//也可以复用
		reserve(v.capacity());
		for (const auto& e:v)
		{
			push_back(e);
		}
		
		
	}

	// vector<T>& operator=(const vector<T>& v){
	// 	if (this != &v)
	// 	{
	// 		delete[] _start;
	// 		_start = new T[v.capacity()];

	// 		memcpy(_start, v._start, sizeof(T) *v.size());
	// 	}
	// 	return *this;
		
	// }

	//现代写法的赋值重载
	vector<T>& operator=(vector<T> v){
		Swap(v);

		return *this;
	}

	void Swap(vector<T>& v){
		swap(_start, v._start);
		swap(_finish, v._finish);
		swap(_end_of_storage, v._end_of_storage);
	}

	// 增容
	void reserve(size_t n)
	{
		size_t capa = capacity();
		if (n > capa)
		{
			size_t len = size();
			T *tmp = new T[n];
			// 第一次进来时_start还是nullptr
			if (_start)
			{
				//memcpy(tmp, _start, sizeof(T) * len);按字节拷贝
				
				for (size_t i = 0; i < len; i++)
				{
					tmp[i] = _start[i];//调用的是T的深拷贝，T为自定义类型
				}
				
				delete[] _start;
			}
			_start = tmp;
			_finish = tmp + len;
			_end_of_storage = tmp + n;
		}
	}

	void resize(size_t n,const T val = T()){
		size_t _size = size();
		size_t _capacity = capacity();

		//如果n小于size
		if(_size > n){
			_finish = _start + n;
		}
		else{
			//如果n大于容量，需要增容
			if(n > capacity){
				reserve(n);
			}
			while (_finish < _start + n)
			{
				*_finish = val;
				++_finish;
			}
			
		}
	}

	void push_back(const T& x)
	{
		if (_finish == _end_of_storage)
		{
			size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
			reserve(newcapacity);
		}
		*_finish = x;
		++_finish;

		//insert(_finish, x);
	}

	void pop_back(){
		if (_finish <= _start)
		{
			cerr << "pop_back()——vector已没有数据" << endl;
			exit(1);
		}
		
		--_finish;

		//erase(_finish - 1);
	}

	void insert(iterator pos,const T& x){
		if (pos > _finish)
		{
			cerr << "insert()——不能超过size插入" << endl;
			exit(1);
		}
		//是否需要增容
		if (_finish == _end_of_storage)
		{
			//需要重新确定位置
			size_t n = pos -_start;
			size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
			reserve(newcapacity);

			//增容后会发生迭代器失效，要确认pos离_start多远，重新赋值
			pos = _start + n;
		}

		//移动数据
		iterator end = _finish -1;
		while(end >= pos){
			*(end + 1) = *end;
			--end;
		}

		*pos = x;
		++_finish;

	}

	iterator erase(iterator pos){
		if (pos > _finish)
		{
			cerr << "erase()——不能超过size插入" << endl;
			exit(1);
		}
		iterator it = pos;
		while (it < _finish)
		{
			*it = *(it + 1);
			it++;
		} 
		--_finish;
		
		return pos;
	}

	T& operator[](size_t i){
		if (i >= size())
		{
			cerr << "operator[]越界访问" << endl;
			exit(1);
		}
		
		return _start[i];
	}

	const T& operator[](size_t i) const{
		if (i >= size())
		{
			cerr << "operator[]越界访问" << endl;
			exit(1);
		}
		
		return _start[i];
	}

	// vector的长度是指向有效数据的尾指针-指向开始数据的指针
	size_t size() const
	{
		return _finish - _start;
	}

	// capacity的长度是指向存储容量的尾指针-指向开始数据的指针
	size_t capacity() const
	{
		return _end_of_storage - _start;
	}

	iterator begin()
	{
		return _start;
	}

	iterator end()
	{
		return _finish;
	}

	const_iterator begin() const
	{
		return _start;
	}

	const_iterator end() const
	{
		return _finish;
	}



	~vector()
	{
		delete[] _start;
		_start = _finish = _end_of_storage = nullptr;
	}

private:
	iterator _start;		  // 指向数据块的开始
	iterator _finish;		  // 指向有效数据的尾
	iterator _end_of_storage; // 指向存储容量的尾
};
